function  [diff, intA, intB]  = compareIntensity(  gsFrame, bwFrame, sktpAll)
%CHECKHEADANDTAIL returns 1 if head and tail orientation of skel is correct
%   
%   Returns the difference in the mean and staandard deviation of intensity between the two end sections 
%   of the worm.

%
%   bwFrame - binary image he worm
%   frame - gray scale image of the worm
%   sktall - skeleton point list for the worm

    %Return result of 100 if the comparison fails
   diff = -100;
   intA = -100;
   intB = -100;
    try
        endpointA = sktpAll(1,:);
        endpointB = sktpAll( size(sktpAll,1),:);
        bwImage = cutworm(bwFrame, sktpAll, 6, [2,6] );
        [r c] = size(bwFrame);
        %Get info for each part of the cut worm
        section = regionprops(bwImage, 'PixelList','Image','BoundingBox');

        %Retrieve intensity information from the head and tail regions
        for i = size(section):-1:1

            %Get rows and collumns of pixels in the region
            myPixelList = [ section(i).PixelList(:,2), section(i).PixelList(:,1) ];

            %If  the region contains the endpointA, get intensity info
            if ismember(endpointA, myPixelList, 'rows') 
                index = sub2ind([r c],myPixelList(:,1),myPixelList(:,2));

                %Create a mask from this region
                mask = zeros(r,c);
                mask(index) = 1;

                %Get intensity info
                endpropsA = regionprops(mask, gsFrame, 'MeanIntensity','MaxIntensity', 'WeightedCentroid');

            end

             %If  the region contains the endpointB, get intensity info
            if ismember(endpointB, myPixelList, 'rows') 
                index = sub2ind([r c],myPixelList(:,1),myPixelList(:,2));

                %Create a mask from this region
                mask = zeros(r,c);
                mask(index) = 1;

                %Get intensity info
                endpropsB = regionprops(mask, gsFrame, 'MeanIntensity','WeightedCentroid');

            end


        end
        intA = endpropsA.MeanIntensity;
        intB = endpropsB.MeanIntensity;
        diff = intA-intB;
        
    catch
       
    end
    
end










